package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.enums.SQLConditionEnum;
import com.gitee.zycra.jdbc.enums.SQLLinkEnum;
import com.gitee.zycra.jdbc.model.PageQueryWrapper;
import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.jdbc.util.SQLBlock;
import com.gitee.zycra.jdbc.util.SQLChain;
import com.gitee.zycra.union.api.common.bo.PageQuery;
import com.gitee.zycra.union.api.common.model.ApiUser;
import com.gitee.zycra.union.api.common.param.PageQueryUserParam;
import org.springframework.stereotype.Repository;

/**
 * @author zycra
 */
@Repository
public class ApiUserDao extends BaseDao<ApiUser> {

    /**
     * 插入
     *
     * @param apiUser 用户对象
     * @return 插入条数
     */
    public int insertApiUser(ApiUser apiUser) {
        return insertIntoTable(apiUser);
    }

    /**
     * 更新
     *
     * @param apiUser 用户对象
     * @return 更新条数
     */
    public int updateApiUser(ApiUser apiUser) {
        return updateTableById(apiUser);
    }

    /**
     * 根据用户ID查询
     *
     * @param userId 用户ID
     * @return 查询到的数据
     */
    public ApiUser selectApiUserById(Long userId) {
        return selectOneTableByNotNullColumnEquals(new ApiUser().setId(userId));
    }

    /**
     * 根据用户名查询
     *
     * @param username 用户名
     * @return 查询到的数据
     */
    public ApiUser selectApiUserByUsername(String username) {
        return selectOneTableByNotNullColumnEquals(new ApiUser().setUsername(username));
    }

    /**
     * 根据条件查询
     *
     * @param pageQuery 查询条件
     * @return 查询到的数据
     */
    public PageResult<ApiUser> selectApiUserForPage(PageQuery<PageQueryUserParam> pageQuery) {
        PageQueryUserParam queryParam = pageQuery.getParam();
        SQLChain sqlChain = SQLChain.builder()
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "username", SQLConditionEnum.EQUALS, queryParam.getUsername()))
                .build();
        PageQueryWrapper param = new PageQueryWrapper(getCountSqlWithOutParam() + sqlChain.getSQL(),
                sqlChain.getParamList(), getSelectAllColumnSql() + sqlChain.getSQL(), sqlChain.getParamList(), pageQuery.getPage(),
                pageQuery.getSize());
        return selectTableForPage(param);
    }
}
